import java.util.*;

class Solution {
    public int countCharacters(String[] words, String chars) {
        int res = 0;
        if (words == null || words.length == 0) return res;
        Map<Character, Integer> charsMap = map(chars);
        for (String word : words) {
            Map<Character, Integer> wordMap = map(word);
            boolean flag = true;
            for (Character k : wordMap.keySet()) {
                Integer v = wordMap.get(k);
                Integer v2 = charsMap.get(k);
                if (v2 == null || v2 < v) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                res += word.length();
            }
        }
        return res;
    }

    private Map<Character, Integer> map(String word) {
        Map<Character, Integer> map = new TreeMap<>();
        for (int i = 0; i < word.length(); i++) {
            char k = word.charAt(i);
            if (map.containsKey(k)) {
                map.put(k, map.get(k) + 1);
            } else {
                map.put(k, 1);
            }
        }
        return map;
    }

    public int countCharacters2(String[] words, String chars) {
        int res = 0;
        if (words == null || words.length == 0) return res;
        int[] arr = new int[26];
        for (int i = 0; i < chars.length(); i++) {
            arr[chars.charAt(i) - 97] += 1;
        }
        for (String word : words) {
            int[] map = arr.clone();
            boolean flag = true;
            for (int i = 0; i < word.length(); i++) {
                int k = word.charAt(i) - 97;
                if (map[k] == 0) {
                    flag = false;
                    break;
                } else {
                    map[k]--;
                }
            }
            if (flag) res += word.length();
        }
        return res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        String[] arr = {"dyiclysmffuhibgfvapygkorkqllqlvokosagyelotobicwcmebnpznjbirzrzsrtzjxhsfpiwyfhzyonmuabtlwin", "ndqeyhhcquplmznwslewjzuyfgklssvkqxmqjpwhrshycmvrb", "ulrrbpspyudncdlbkxkrqpivfftrggemkpyjl", "boygirdlggnh", "xmqohbyqwagkjzpyawsydmdaattthmuvjbzwpyopyafphx", "nulvimegcsiwvhwuiyednoxpugfeimnnyeoczuzxgxbqjvegcxeqnjbwnbvowastqhojepisusvsidhqmszbrnynkyop", "hiefuovybkpgzygprmndrkyspoiyapdwkxebgsmodhzpx", "juldqdzeskpffaoqcyyxiqqowsalqumddcufhouhrskozhlmobiwzxnhdkidr", "lnnvsdcrvzfmrvurucrzlfyigcycffpiuoo", "oxgaskztzroxuntiwlfyufddl", "tfspedteabxatkaypitjfkhkkigdwdkctqbczcugripkgcyfezpuklfqfcsccboarbfbjfrkxp", "qnagrpfzlyrouolqquytwnwnsqnmuzphne", "eeilfdaookieawrrbvtnqfzcricvhpiv", "sisvsjzyrbdsjcwwygdnxcjhzhsxhpceqz", "yhouqhjevqxtecomahbwoptzlkyvjexhzcbccusbjjdgcfzlkoqwiwue", "hwxxighzvceaplsycajkhynkhzkwkouszwaiuzqcleyflqrxgjsvlegvupzqijbornbfwpefhxekgpuvgiyeudhncv", "cpwcjwgbcquirnsazumgjjcltitmeyfaudbnbqhflvecjsupjmgwfbjo", "teyygdmmyadppuopvqdodaczob", "qaeowuwqsqffvibrtxnjnzvzuuonrkwpysyxvkijemmpdmtnqxwekbpfzs", "qqxpxpmemkldghbmbyxpkwgkaykaerhmwwjonrhcsubchs"};
        String chars = "usdruypficfbpfbivlrhutcgvyjenlxzeovdyjtgvvfdjzcmikjraspdfp";
        int res = s.countCharacters(arr, chars);
        System.out.println(res);
    }
}
